package com.shallwead.bna;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

import com.shallwead.bna.Async.AdInstallAsync;
import com.shallwead.bna.Async.PushInstallAsync;
import com.shallwead.bna.object.AdTracker;
import com.shallwead.bna.util.SharedPreferenceUtil;

public class BnAReceiver extends BroadcastReceiver {

	@Override
	public void onReceive(Context context, Intent intent) {
		BnA bna = new BnA(context.getApplicationContext());
		// Logger.e("== onReceive1 " + intent);
		// Logger.e("== OnReceive2 " + intent.getAction());
		String pkg = SharedPreferenceUtil.getStringSharedPreference(context,
				"adpkg");
		String adIdx = SharedPreferenceUtil.getStringSharedPreference(context,
				"adidx");
		String pushidx = SharedPreferenceUtil.getStringSharedPreference(
				context, "pushidx");
		String u = SharedPreferenceUtil.getStringSharedPreference(context, "u");

		if (intent.getAction().compareTo(Intent.ACTION_PACKAGE_ADDED) == 0) {

			if (intent.getDataString().contains(pkg)) {

				if (!"".equals(adIdx)) {
					AdTracker adTracker = new AdTracker(adIdx, BnA.pkg, u);
					new AdInstallAsync(context, adTracker).execute();
				}
				if (!"".equals(pushidx)) {
					AdTracker adTracker = new AdTracker(pushidx, BnA.pkg, u);
					new PushInstallAsync(context, adTracker).execute();
				}
			}
		}

		if (intent.getAction().compareTo(Intent.ACTION_PACKAGE_REMOVED) == 0) {
			if (intent.getDataString().contains("com.swa")) {
				// context.startActivity(new Intent(Intent.ACTION_VIEW, Uri
				// .parse("http://www.naver.com"))
				// .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
			}
		}

	}
}
